﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using wUniAppPCL.IO;

namespace wUniAppPCL.MVVM
{
  public class PatientsXml : wSerializer<PatientsViewModel, wXmlSerializer<PatientsViewModel>>
  {
    public PatientsXml(IwStream streamLoader)
    {
      stream = streamLoader;
    }

    IwStream stream;

    public Task<PatientsViewModel> Load()
    {
      Task<PatientsViewModel> t = new Task<PatientsViewModel>(() =>
        {
          try
          {
            using (Stream stm = stream.GetStreamForRead())
            {
              return Deserialize(stm);
            }
          }
          catch (Exception ex)
          {
            wLogger.ToLog(ex);
            return new PatientsViewModel();
          }
        }
      );
      t.Start();
      return t;
    }

    public Task Save(PatientsViewModel model)
    {
      Task t = new Task(() =>
      {
         try
         {
           using (Stream stm = stream.GetStreamForWrite())
           {
             Serialize(stm, model);
           }
         }
         catch (Exception ex)
         {
           wLogger.ToLog(ex);
         }
       });
      t.Start();
      return t;
    }
  }

}
